/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air.pagehandler;

import air.sb.AirRemote;
import air.sb.LuftRemote;
import air.sb.SASRemote;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;

/**
 *
 * @author mrxyz
 */
public class ReceiptBean {

    AirRemote AR = lookupAirBean();
    LuftRemote LR = lookupLuftBean();
    SASRemote SR = lookupSASBean();

    public String getReceipt() {
        int tripNr;
        if((tripNr = AR.getTripNr()) == 0)
            if((tripNr = LR.getTripNr()) == 0)
                tripNr = SR.getTripNr();
        String rec = null;
        if ((rec = AR.getReceipt(tripNr)) == null) {
            if ((rec = LR.getReceipt(tripNr)) == null) {
                rec = SR.getReceipt(tripNr);
            }
        }
        return rec;
    }

    private AirRemote lookupAirBean() {
        try {
            Context c = new InitialContext();
            return (AirRemote) c.lookup("java:comp/env/AirBean");
        } catch (NamingException ne) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private LuftRemote lookupLuftBean() {
        try {
            Context c = new InitialContext();
            return (LuftRemote) c.lookup("java:comp/env/LuftBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private SASRemote lookupSASBean() {
        try {
            Context c = new InitialContext();
            return (SASRemote) c.lookup("java:comp/env/SASBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
